#ifndef NVR_H
#define NVR_H

#include <QThread>
#include <QEventLoop>
#include "oeasycommon.h"
#include "configfile.h"
#include "HCNetSDK.h"
#include "PlayM4.h"
#include <QString>
#include <QByteArray>
#include <QMap>
#include <QList>
#include <QDateTime>
#include <QTimer>
#include <time.h>
#include <string.h>


typedef struct tagSTRUCT_SNAPANDVIDEO {   
    LONG lRealPlayHandle=-1;
    int hPlayback=-1;//下载句柄
    int channel;     //nvr通道偏移
    QByteArray data=NULL;
    time_t stime;  //开门时间到来时pc时间
    time_t nvrTime;//开门时间到来时从nvr读取的时间
    int nPos=0;    //video下载进度
    int jpgStep=0; //抓拍第一张标志
    int videoUpdateSta=0;//NET_DVR_UpdateRecordIndex是否成功，0不成公功，1成功
    int videoUpdateT=0;//NET_DVR_UpdateRecordIndex次数
    int videoStep=0;// 下载视频状态，0未开始，1正在下载，2下载完成
    bool hadSend=false; //是否已发送
    bool getNVRTimeOK=false;//是否已成功从nvr取得时间
    char jpg1[256];
    char jpg2[256];
    char jpg3[256];
    QString filePath=NULL;
}STRUCT_SNAPANDVIDEO;

class NVR : public QThread
{
    Q_OBJECT
public:
    explicit NVR(STRUCT_NVR_INFO &nvrInfo);
    ~NVR();
    LONG m_lUserID;
    STRUCT_NVR_INFO &m_nvrInfo;
    bool m_devState; //设备在线状态
private:
    ConfigFile m_cfg;
    int m_startDChan;//起始IP通道
    int m_IPChanNum;//IP最大通道数量
    QList<STRUCT_SNAPANDVIDEO> m_openList;
    QEventLoop *m_eventLoop;
    void devInit();
    void devAntiInit();
    int downLoadVideo();
    int saveFile(QString fileName,QByteArray jpg);
    time_t getNVRTime();

protected:
    void run();

signals:
    void loginOver(long, long, int, int);
    void snapAndGetVideoOK(QByteArray, QString, QString, QString, QString);
    void sigSnapAndGetVideo(QByteArray,int,LONG);
public slots:
    void onLoginOver(long lUserID, long dwResult, int startDChan,int iPChanNum);
    void onTimeout();
    void onSnapAndGetVideo(QByteArray data,int channel,LONG lRealPlayHandle);
};

#endif // NVR_H
